"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from django.http import  HttpResponse
from regist.models import Restaurant
from common.json import packIntoJsonData
from common.error import commonResponse
from django.test import TestCase
from mysession.views import *
from views import logincheck, registcommit,getrestaurantinfo
import json

class REQUEST():
    def __init__(self):
        self.POST = {}
        self.POST['username'] = None
        self.POST['password'] = None
        self.POST['email'] = None
        self.POST['restaurantid'] = 1
        self.session = {}
        self.session['_auth_user_id'] = 1
        self.user = {}
       
    
class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        #add a register
        request = REQUEST()
        request.POST['username'] = "user"
        request.POST['password'] = "123456"
        request.POST['email'] = "user@test.com"
        
                
        registcommit( request )
        
        #login
        logincheck(request)
        #add a restaurant
        addrestaurant(self) 
        
        request.POST['queuerestaurantpk'] = 1
        
        Ret = getrestaurantinfo(request)
        err2 = Ret._container
        print err2
        err = json.loads(err2[0])
        print err['error']
        self.assertEqual(err['error'] , 0 )
        
        Ret = queryRestaurantQueueInfo(request)
        err2 = Ret._container
        print err2
        err = json.loads(err2[0])
        print err['error']
        self.assertEqual(err['error'] , 0 )
        
        orderstart(request)
        
        Ret = requestapassport(request)  #get a number 
        err2 = Ret._container
        print err2
        err = json.loads(err2[0])
        print err['error']
        self.assertEqual(err['error'] , 0 )
        
        Ret = kickoutapassport(request) #pick out the first one in line
        err2 = Ret._container
        print err2
        err = json.loads(err2[0])
        print err['error']
        self.assertEqual(err['error'] , 0 )
        
        Ret = orderstop(request) #stop 
        
        err2 = Ret._container
        print err2
        err = json.loads(err2[0])
        print err['error']
        self.assertEqual(err['error'] , 0 )

        
def addrestaurant(self):
    Result = commonResponse()
    
    newres = Restaurant(
                        name = "restaurantname12", 
                        anothername = "anothername9", 
                        tastestyle= 9, 
                        address = "address9", 
                        nearby = "nearby9", 
                        gpsposition_x = 9, 
                        gpsposition_y = 9, 
                        phone1 = 18601612699, 
                        phone2 = 1234567899, 
                        open = 9, 
                        close = 29, 
                        howtogetthere = "howtogetthere9", 
                        administrator = "user",
                        queuestartflag = False,
                        queuenextcallnum = 0,
                        queuenextdistributenum = 0
                        )
       
    if newres is not None:
        newres.save(True)
        Result.result = 0
        
    else:
        Result.result = -1
        
    #self.assertEqual(count + 1, Restaurant.objects.count() )
    return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
    
